import { client } from '../../request/index.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    ip: getApp().globalData.ip,
    imgIp: getApp().globalData.imgIp,
    user_name: '',
    needMoney: '',
    location: '',
    title: '',
    detail: '',
    size: { maxHeight: 500, minHeight: 100 },
    imgBox: [],
    uri: [],
  },
  // 查看图片
  viewImg(e) {
    console.log(e)
    let imgUrl = e.currentTarget.dataset.url
    wx.previewImage({
      urls: [imgUrl],
      current: '',
      success: function (res) {},
    })
  },
  // 添加图片
  chooseImage() {
    let that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: '#000000',
      success(res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.addImg('album')
          } else if (res.tapIndex == 1) {
            that.addImg('camera')
          }
        }
      },
    })
  },
  addImg(type) {
    let that = this
    let imgBox = that.data.imgBox
    wx.chooseImage({
      count: 1, // 最多1张
      sizeType: ['compressed'],
      sourceType: [type],
      success(res) {
        let tempFilePaths = res.tempFilePaths
        imgBox = imgBox.concat(tempFilePaths)
        that.setData({
          imgBox: imgBox,
        })
      },
    })
  },
  //删除图片
  imgDelete(e) {
    let that = this
    let index = e.currentTarget.dataset.deindex
    let imgBox = that.data.imgBox
    imgBox.splice(index, 1)
    that.setData({
      imgBox: imgBox,
    })
  },
  //表单验证
  check(e) {
    if (e.detail.value.user_name == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入姓名',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (e.detail.value.needMoney == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入所需金额',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (e.detail.value.location == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入详细地址',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (e.detail.value.title == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入标题',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (e.detail.value.detail == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入详细内容',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (this.data.imgBox.length == 0) {
      Dialog.alert({
        title: '提示',
        message: '请上传照片',
        confirmButtonText: '返回上传',
      })
      return false
    }
    return true
  },
  // 提交
  formSubmit(e) {
    let that = this
    // 表单验证
    if (!that.check(e)) return
    wx.uploadFile({
      url: getApp().globalData.ip + '/upload',
      filePath: that.data.imgBox[0],
      name: 'file',
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
        'content-type': 'charset=utf-8',
      },
      dataType: 'json',
      success(res) {
        let datas = JSON.parse(res.data)
        let new_url = that.data.uri.concat(datas.data)
        that.setData({
          uri: new_url,
        })
        console.log('参数', e.detail.value)
        console.log('图片', that.data.uri)
        that.uploadData(e.detail.value)
      },
    })
  },

  async uploadData(v) {
    let that = this
    let money = parseInt(v.needMoney)
    const getData = await client({
      url: '/poor-man-info/insert',
      method: 'POST',
      data: {
        user_name: v.user_name,
        needMoney: money,
        location: v.location,
        title: v.title,
        content: v.detail,
        coverPicUrl: that.data.uri[0],
      },
    })
    console.log('提交返回:', getData)
    that.onLoad()
    wx.showToast({
      title: '提交成功',
      icon: 'success',
    })
  },

  onLoad: function (options) {
    this.setData({
      user_name: '',
      needMoney: '',
      location: '',
      title: '',
      detail: '',
      imgBox: [],
      uri: [],
    })
  },
})
